package ru.st.selenium;

import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;

import org.testng.*;
import org.testng.annotations.*;

import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class Search extends ru.st.selenium.pages.TestBase {
  private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();

  @Test
  public void testSearch() throws Exception { 
	  driver.get(baseUrl + "/php4dvd/");
		driver.findElement(By.id("username")).clear();
		driver.findElement(By.id("username")).sendKeys("admin");
		driver.findElement(By.name("password")).clear();
		driver.findElement(By.name("password")).sendKeys("admin");
		driver.findElement(By.name("submit")).click();
	    
	    driver.findElement(By.cssSelector("img[alt=\"Add movie\"]")).click();
	    driver.findElement(By.id("imdbsearch")).clear();
	    driver.findElement(By.id("imdbsearch")).sendKeys("my first");
	    driver.findElement(By.name("imdbid")).clear();
	    driver.findElement(By.name("imdbid")).sendKeys("45436");
	    driver.findElement(By.name("name")).clear();
	    driver.findElement(By.name("name")).sendKeys("my first");
	    driver.findElement(By.name("aka")).clear();
	    driver.findElement(By.name("aka")).sendKeys("my first");
	    driver.findElement(By.name("year")).clear();
	    driver.findElement(By.name("year")).sendKeys("1989");
	    driver.findElement(By.id("submit")).click();
	    driver.findElement(By.cssSelector("h1")).click();
	    driver.findElement(By.id("q")).clear();
        driver.findElement(By.id("q")).sendKeys("my first");
        driver.findElement(By.id("q")).sendKeys(Keys.RETURN);
        String search=driver.findElement(By.className("title")).getText();
        assertEquals("my first", search);
        driver.findElement(By.id("q")).clear();
        driver.findElement(By.id("q")).sendKeys("my first2");
        driver.findElement(By.id("q")).sendKeys(Keys.RETURN);
        String nosearch=driver.findElement(By.className("content")).getText();
        assertEquals("No movies where found.", nosearch);
        
  }

  private boolean isElementPresent(By by) {
    try {
      driver.findElement(by);
      return true;
    } catch (NoSuchElementException e) {
      return false;
    }
  }

  private String closeAlertAndGetItsText() {
    try {
      Alert alert = driver.switchTo().alert();
      String alertText = alert.getText();
      if (acceptNextAlert) {
        alert.accept();
      } else {
        alert.dismiss();
      }
      return alertText;
    } finally {
      acceptNextAlert = true;
    }
  }
}

